package il.ac.braude.sona.gui.forms;


import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.gui.forms.questions.FormQuestion;

import java.awt.Component;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * The FormPanel class contains all methos to create a panel that contains fields for user to fill
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */

public class FormPanel extends ImagePanel {
	private static final long serialVersionUID = 1L;
	/**
	 * mQuestions - ArrayList of the FormQuestion that the panel contains
	 */
	private ArrayList<FormQuestion> mQuestions = new ArrayList<FormQuestion> ();
	/**
	 * mePanel - The "main" panel of the form
	 */
	private JPanel mePanel;
	
	/**
	 * The DefaultQuestion define the deafault kind of question
	 *  
	 * @author Ofek Ron
	 * @author Avihay Avitan
	 * @author Nitzan Inbar
	 * @author Sasha Rozenthal
	 */
	private class DefaultQuestion extends FormQuestion{

		private static final long serialVersionUID = 1L;
		/**
		 * tf - The question in JTextField versa
		 */
		JTextField tf = new JTextField(20);
		/**
		 * Constructs the DefaultQuestion.
		 * 
		 * @param question - the question name to create
		 */
		DefaultQuestion(String question) {
			super(BoxLayout.X_AXIS);
			setOpaque(false);
			setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
			JLabel q = new JLabel(question);
			q.setOpaque(false);
			q.setLabelFor(tf);
			add(q);
			add(tf);
			
		}
		/**
		 * Define if the question area can be editable
		 * @param isEditable -true for editable, else false
		 */
		@SuppressWarnings("unused")
		void setEditable(boolean isEditable) {
			tf.setEditable(isEditable);
		}
		/**
		 * setting the default answer when submiting the form
		 * @param defaultAnswer -the default answer to be
		 */
		@Override
		public void setDefaultAnswer(Object defaultAnswer) {
			tf.setText(defaultAnswer.toString());
		}
		/**
		 * getting the text from tf TextField
		 * @return Object -the text that present in tf
		 */
		@Override
		public Object getAnswer() {
			return tf.getText();
		}

	}
	/**
	 * Constructs the FormPanel.
	 * 
	 * @param questionKeys - the questions to add to the formPanel
	 * @param bg - Background Image
	 */
	public FormPanel(String[] questionKeys, Image bg) {
		super(bg);
		init(questionKeys);
	}
	/**
	 * Constructs the FormPanel.
	 * 
	 * @param questionKeys - the questions to add to the formPanel
	 */
	public FormPanel(String[] questionKeys) {
		init(questionKeys);
	}
	/**
	 * Constructs the FormPanel.
	 */
	public FormPanel() {
		setOpaque(false);
		JScrollPane sp = new JScrollPane();
		mePanel=new JPanel();
		mePanel.setLayout(new BoxLayout(mePanel,BoxLayout.Y_AXIS));
		sp.getViewport().add(mePanel);
		add(sp);
	}
	/**
	 * adding questions to the panel 
	 * 
	 * @param questionKeys - the questions to add to the formPanel
	 */
	private void init(String[] questionKeys) {
		setOpaque(false);
		
		//mConstraints = new GridBagConstraints();
		for  (int i = 0 ; i < questionKeys.length ; i++) 
			addQuestion(questionKeys[i]);
		
	}
	/**
	 * runns over all questions that in the form and gets their current answer
	 * 
	 * @return results - ArrayList of answers to all questions in the form
	 */
	protected ArrayList<Object> getResult() {
		ArrayList<Object> results = new ArrayList<Object>();
		boolean isFilled = true;
		for (FormQuestion q : mQuestions) {
			Object ans = q.getAnswer();
			results.add(ans);
			if (q.isNecessary() && ans==null) {
				isFilled=false;
				q.markQuestion(); 
			}
			else q.unmarkQuestion();
		}
		return isFilled? results : null;
	}
	/**
	 * adding question to the panel 
	 * 
	 * @param question - the question to add to the formPanel
	 */
	public void addQuestion(String question) {
		FormQuestion q = new DefaultQuestion(question);
		mQuestions.add(q);
		mePanel.add(q);
	}
	/**
	 * adding questions to the panel 
	 * 
	 * @param questionKeys - the questions to add to the formPanel
	 */
	public void addQuestions(String[] questionKeys) {
		for  (int i = 0 ; i < questionKeys.length ; i++) 
			addQuestion(questionKeys[i]);
	}
	
	/**
	 * adding question to the panel 
	 * 
	 * @param question - the question to add to the formPanel
	 */
	public void addQuestion(FormQuestion question) {
		mQuestions.add(question);
		mePanel.add(question);
	}
	/**
	 * adding title with underline to the form
	 * 
	 * @param title - the title to add to the formPanel
	 */
	public void addTitleWithUnderline(String title) {
		JPanel centerTitle=new JPanel();
		JLabel label=new JLabel("<html><u>"+title+"</u></html>");;
		label.setFont(Resources.Futura20);
		centerTitle.add(Box.createHorizontalGlue());
		centerTitle.add(label);
		centerTitle.add(Box.createHorizontalGlue());
		mePanel.add((centerTitle));
	}
	/**
	 * adding title to the form
	 * 
	 * @param title - the title to add to the formPanel
	 */
	public void addTitle(String title) {
		JPanel centerTitle=new JPanel();
		JLabel label=new JLabel(title);
		label.setFont(Resources.Futura18);
		centerTitle.add(Box.createHorizontalGlue());
		centerTitle.add(label);
		centerTitle.add(Box.createHorizontalGlue());
		mePanel.add((centerTitle));
	}
	/**
	 * setting the default answer to the questions in the form
	 * @param values -the answers from the form
	 */
	public void setDefaultValues(ArrayList<String> values) {
		int size = mQuestions.size();
		for  (int i = 0 ; i < size ; i++) {
			mQuestions.get(i).setDefaultAnswer(values==null || i >= values.size() ? "" : values.get(i));
		}	
	}



}

